package com.ruyuan.twelve.juc._15.promise;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Test {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        long startTime = System.currentTimeMillis();

        //执行支线任务并且获取promise
        FutureTask<Object> promise = Promisor.create(startTime);

        //主线任务执行茶杯茶叶任务
        System.out.println("准备执行茶叶茶杯，需要3分钟，当前用时"+(System.currentTimeMillis()-startTime)+"ms");
        TeaAndCup teaAndCup = new TeaAndCup();
        Thread.sleep(3000);
        teaAndCup.setStatus(true);
        System.out.println("执行茶叶茶杯结束，当前用时"+(System.currentTimeMillis()-startTime)+"ms");

        //根据promise获取执行结果
        //这个接口不会阻塞
        if (!promise.isDone()){
            System.out.println("等待烧水完成，当前用时"+(System.currentTimeMillis()-startTime)+"ms");
        }

        //若任务未完成，该接口会阻塞
        BoilWater boilWater = (BoilWater)promise.get();
        System.out.println("烧水完毕，当前用时"+(System.currentTimeMillis()-startTime)+"ms");
        System.out.println("开始泡茶");
        System.out.println("泡茶完成，当前用时"+(System.currentTimeMillis()-startTime)+"ms");

    }
}
